package com.jvm;

/**
 * 描述:
 * 类型强转换
 * @author luzhongfu
 * Created by lucky on 2022-03-01 10:38 AM
 */
public class CastDemo {

    public static void main(String[] args) throws IllegalAccessException, InstantiationException {
        BmCar bmCar = new BmCar();

        Car car = Car.class.cast(bmCar);

        car.say();

//        BcCar bc = BcCar.class.cast(bmCar);
//        bc.say();

        Class<?> bmCarClass = BmCar.class;
        Object bmCar1 = bmCarClass.newInstance();

        Car cast2 = (Car) bmCar1;
        Car cast = Car.class.cast(bmCar1);
        cast.say();
    }

    static class Car{

        public void say(){}
    }

    static class BmCar extends Car{
        @Override
        public void say(){
            System.out.println("宝马");
        }
    }

    static class BcCar extends Car{
        @Override
        public void say(){
            System.out.println("奔驰");
        }
    }
}
